package zhoug.zui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import zhoug.logging.Logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 可以限制最大宽高和最小宽高的RecyclerView
 * @Author 35574
 * @Date 2022/4/29
 * @Description
 */
public class ZUIRecyclerView extends RecyclerView {
    private static final String TAG = ">>>ZUIRecyclerView";
    private static boolean debug = true;

    private int minHeight;//最小高度
    private int minWidth;//最小宽度
    private int maxHeight;//最大高度
    private int maxWidth;//最大宽度

    public ZUIRecyclerView(@NonNull Context context) {
        this(context, null);
    }

    public ZUIRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        parseAttr(context, attrs, 0);
    }

    public ZUIRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        parseAttr(context, attrs, defStyleAttr);

    }

    private void parseAttr(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZUIRecyclerView, defStyleAttr, 0);
        minHeight = a.getDimensionPixelOffset(R.styleable.ZUIRecyclerView_zui_minHeight, 0);
        minWidth = a.getDimensionPixelOffset(R.styleable.ZUIRecyclerView_zui_minWidth, 0);
        maxHeight = a.getDimensionPixelOffset(R.styleable.ZUIRecyclerView_zui_maxHeight, 0);
        maxWidth = a.getDimensionPixelOffset(R.styleable.ZUIRecyclerView_zui_maxWidth, 0);
        a.recycle();
    }


    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        super.onMeasure(widthSpec, heightSpec);
        if (minHeight > 0 || minWidth > 0 || maxHeight > 0 || maxWidth > 0) {
            int width = getMeasuredWidth();
            int height = getMeasuredHeight();
            if (debug) {
                Logger.d(TAG, "onMeasure:measuredWidth=" + width + ",measuredHeight=" + height);
            }
            int widthMode = MeasureSpec.getMode(widthSpec);
            int heightMode = MeasureSpec.getMode(heightSpec);
            if (widthMode != MeasureSpec.EXACTLY) {
                if (minWidth > 0) {
                    width = Math.max(minWidth, width);
                }
                if (maxWidth > 0) {
                    width = Math.min(maxWidth, width);
                }
            }
            if (heightMode != MeasureSpec.EXACTLY) {
                if (minHeight > 0) {
                    height = Math.max(minHeight, height);
                }
                if (maxHeight > 0) {
                    height = Math.min(maxHeight, height);
                }
            }
            if (debug) {
                Logger.d(TAG, "onMeasure:width=" + width + ",height=" + height);
            }
            setMeasuredDimension(width, height);
        }


    }

    public void setMinHeight(int minHeight) {
        this.minHeight = minHeight;
    }

    public void setMinWidth(int minWidth) {
        this.minWidth = minWidth;
    }

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }

    public void setMaxWidth(int maxWidth) {
        this.maxWidth = maxWidth;
    }

}
